package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsUser;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.User;
import com.example.xpvideo.entity.req.UserListReq;
import com.example.xpvideo.entity.res.PhpUserListRes;
import com.example.xpvideo.entity.res.UserListRes;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsUserMapper;
import com.example.xpvideo.mapper.PhpUserMapper;
import com.example.xpvideo.service.PhpUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/10/18 10:47
 * @description:
 */
public class PhpUserServiceImpl implements PhpUserService {
    @Autowired
    private PhpUserMapper phpUserMapper;
    @Autowired
    private LsUserMapper lsUserMapper;

    @Override
    public ResponseResult<PageResult<List<PhpUserListRes>>> list(UserListReq userListReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String account = userListReq.getAccount();
        String nickname = userListReq.getNickname();
        String superAccount = userListReq.getFirstLeader();
        String indirectSuperAccount = userListReq.getSecondLeader();

        PageHelper.startPage(page, size);

        List<PhpUserListRes> list = phpUserMapper.UserList(Wrappers.<LsUser>query()
                .eq(!ObjectUtils.isEmpty(account),"account",account)
                .like(!ObjectUtils.isEmpty(nickname),"nickname",nickname)
                .eq(!ObjectUtils.isEmpty(superAccount), "first_leader",superAccount)
                .eq(!ObjectUtils.isEmpty(indirectSuperAccount), "second_leader",indirectSuperAccount));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpUserListRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }
}
